Golang bufio.ScanWords() function example

package bufio

ScanWords is a split function for a Scanner that returns each space-separated word of text, with surrounding spaces deleted. It will never return an empty string. The definition of space is set by unicode.IsSpace.

Golang bufio.ScanWords() function usage example

 file, err := os.Open("words.txt")

 if err != nil {
 panic(err.Error())
 }

 defer file.Close()

 reader := bufio.NewReader(file)
 scanner := bufio.NewScanner(reader)

 scanner.Split(bufio.ScanWords)

 for scanner.Scan() {

 fmt.Printf("%s ", scanner.Text())

 }

Reference :

http://golang.org/pkg/bufio/#ScanWords

Advertisement